/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.hr.cmi.android.aanwezigheidsregistratie.adapter;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 *
 * @author Johan
 */
public class StudentsDBAdapter {

    public static final String KEY_NAAM = "naam";
    public static final String KEY_ROWID = "_studentnummer";
    private static final String TAG = "StudentsDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;
    /**
     * Database creation sql statement
     */
    private static final String DATABASE_CREATE =
            "create table studenten (_studentnummer INT primary key, "
            + "naam VARCHAR(255) not null);";
    private static final String DATABASE_NAME = "data";
    private static final String DATABASE_TABLE = "studenten";
    private static final int DATABASE_VERSION = 2;
    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
    }

    public StudentsDBAdapter(Context ctx) {
        this.mCtx = ctx;
    }

    public StudentsDBAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }

    public long createNote(long studentnummer, String naam) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_ROWID, studentnummer);
        initialValues.put(KEY_NAAM, naam);
        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

    public boolean deleteNote(long rowId) {
        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    public Cursor fetchAllNotes() {
        return mDb.query(DATABASE_TABLE, new String[]{KEY_ROWID, KEY_NAAM}, null, null, null, null, null);
    }

    public Cursor fetchNote(long rowId) throws SQLException {
        Cursor mCursor =
                mDb.query(true, DATABASE_TABLE, new String[]{KEY_ROWID,
                    KEY_NAAM}, KEY_ROWID + "=" + rowId, null,
                null, null, null, null);
        Log.e("StudentsDBAdapter", mCursor.getCount() + "");
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

    public boolean updateNote(long rowId, String naam) {
        ContentValues args = new ContentValues();
        args.put(KEY_NAAM, naam);
        return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}
